package cc.linker.steplink.crm.merchant.controller;


import cc.linker.steplink.crm.commons.model.PcUserInfo;
import cc.linker.steplink.crm.merchant.pojo.menu.PublicMenuPo;
import cc.linker.steplink.crm.merchant.pojo.menu.ResourceListPo;
import cc.linker.steplink.crm.merchant.pojo.menu.ResourcePo;
import cc.linker.steplink.crm.merchant.service.PublicMenuService;
import cc.linker.steplink.crm.merchanth5.commons.UserInfoUtil;
import cc.linker.steplink.crm.response.GlobalException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;

/**
 * 公众号菜单
 * @author huhj
 */
@Slf4j
@RestController
@RequestMapping("/pc/publicmenu")
public class PublicMenuController {

    @Autowired
    private PublicMenuService publicMenuService;
    @Autowired
    private UserInfoUtil userInfoUtil;

    /**
     * 同步菜单
     *
     * @param
     * @throws IOException
     * @throws URISyntaxException
     */
    @GetMapping("/syncmenu")
    public Map<String, Object> syncMenu(HttpServletRequest request) throws IOException, URISyntaxException {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        return publicMenuService.syncMenu(userinfo.getOrgId());
    }


    /**
     * 获取公众号当前使用的自定义菜单的配置
     *
     * @param
     * @return
     * @throws IOException
     * @throws URISyntaxException
     */
    @GetMapping("/getcurrentselfmenu")
    public Map<String, Object> getCurrentSelfMenu(HttpServletRequest request) throws IOException, URISyntaxException {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        return publicMenuService.getCurrentSelfMenu(userinfo.getOrgId());
    }

    /**
    * @Description：获取个性化菜单信息
    * @DateTime：2020/8/28 10:40
    * @Params：
    * @Return：
    */
    @GetMapping("/getIndividualityMenuInfo")
    public PublicMenuPo getIndividualityMenuInfo(@RequestParam Long menuId) {
        if (menuId == null) {
            throw GlobalException.build("菜单ID不能为空");
        }
        return this.publicMenuService.getIndividualityMenuInfo(menuId);
    }

    /**
     * @Description：添加个性化菜单
     * @DateTime：2020/8/24 15:10
     * @Params：
     * @Return：
     */
    @PostMapping("/insertIndividualityMenu")
    public void insertIndividualityMenu(@RequestBody PublicMenuPo publicMenuPo, HttpServletRequest request) {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        this.publicMenuService.insertIndividualityMenu(publicMenuPo, userinfo.getOrgId());
    }

    /**
     * @Description：编辑个性化菜单
     * @DateTime：2020/8/24 15:10
     * @Params：
     * @Return：
     */
    @PostMapping("/updateIndividualityMenu")
    public void updateIndividualityMenu(@RequestBody PublicMenuPo publicMenuPo,HttpServletRequest request) {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        this.publicMenuService.updateIndividualityMenu(publicMenuPo, userinfo.getOrgId());
    }

    /**
    * @Description：调整顺序
    * @DateTime：2020/8/27 14:56
    * @Params：
    * @Return：
    */
    @PostMapping("/updateIndividualityMenuSort")
    public void updateIndividualityMenuSort(@RequestBody Map<String,Object> param , HttpServletRequest request) {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        Long menuId = Long.valueOf(param.get("menuId").toString());
        Integer moveType = Integer.valueOf(param.get("moveType").toString());
        this.publicMenuService.updateIndividualityMenuSort(menuId,moveType,userinfo.getOrgId());
    }

    @GetMapping("/testRunCalc")
    public void testRunCalc() {
        this.publicMenuService.runCustomerTask();
    }

    /**
     * @Description：获取个性化菜单列表
     * @DateTime：2020/8/24 15:52
     * @Params：
     * @Return：
     */
    @GetMapping("/getIndividualityMenuList")
    public List<PublicMenuPo> getIndividualityMenuList(HttpServletRequest request) {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        return this.publicMenuService.getIndividualityMenuList(userinfo.getOrgId());
    }

    /**
    * @Description：删除个性化菜单
    * @DateTime：2020/8/25 11:12
    * @Params：
    * @Return：
    */
    @DeleteMapping("/deleteIndividualityMenuByMenuId")
    public void deleteIndividualityMenuByMenuId(@RequestParam Long menuId, HttpServletRequest request) {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        this.publicMenuService.deleteIndividualityMenuByMenuId(menuId, userinfo.getOrgId());
    }


    /**
     * 发布菜单
     * @param
     * @param param
     * @throws IOException
     * @throws URISyntaxException
     */
    @PostMapping("/publishmenu")
    public void publishMenu(HttpServletRequest request, @RequestBody Map<String,Object>param) throws IOException, URISyntaxException {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        param.put("orgId",userinfo.getOrgId());
        publicMenuService.publishMenu(param);
    }

    /**
     * 删除公众号所有菜单
     * @param
     * @throws IOException
     * @throws URISyntaxException
     */
    @PostMapping("/allMenuDelete")
    public void allMenuDelete(HttpServletRequest request) throws IOException, URISyntaxException {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        publicMenuService.allMenuDelete(userinfo.getOrgId());
    }

    /**
     * 获取公众号认证状态
     * @param
     * @return
     * @throws IOException
     * @throws URISyntaxException
     */
    @GetMapping("/getpublicverify")
    public boolean getPublicVerify(HttpServletRequest request) throws IOException, URISyntaxException {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        return publicMenuService.getPublicVerify(userinfo.getOrgId());
    }

    /**
     * 获取菜单同步状态
     * @param
     * @return
     */
    @GetMapping("/getSyncStatus")
    public Map<String,Object> getSyncStatus(HttpServletRequest request){
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        return publicMenuService.getSyncStatus(userinfo.getOrgId());
    }

    /**
     * 获取机构公众号信息
     * @param
     * @return
     */
    @GetMapping("/getPublicInfo")
    public Map<String,Object> getPublicInfo(HttpServletRequest request){
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        return publicMenuService.getPublicInfo(userinfo.getOrgId());
    }

    @PostMapping("/vaildResourceExit")
    public List<ResourcePo> vaildResourceExit(@RequestBody ResourceListPo resList) {
        return this.publicMenuService.vaildResourceExit(resList);
    }

    public void sendMessage(String paramStr){
        try{
            publicMenuService.sendMessage(paramStr);
        }catch (Exception e){
            LOGGER.error(" 菜单事件推送异常！", e);
        }

    }

}
